package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description NursingPlanImpl
 * @Author mzx
 * @Date 2024-07-08
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    //护理计划分页查询
    @Override
    public PageResponse<NursingPlanVo> getBypage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.getBypage(name, status);

        for (NursingPlanVo nursingPlanVo : page) {
            nursingPlanVo.setCount(nursingPlanVo.getLid() == null ? 0 : 1) ;
        }

        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    //新增护理计划
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //先新增到护理计划表中，同时返回计划id
        nursingPlanMapper.addPlan(nursingPlan);
        //添加护理计划中的护理项目
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingPlan.getProjectPlans();
        //如果集合有数据
        if (!CollectionUtils.isEmpty(nursingProjectPlanVoList)) {
            for (NursingProjectPlanVo projectPlan : nursingProjectPlanVoList) {
                projectPlan.setCreateTime(LocalDateTime.now());
                projectPlan.setPlanId(nursingPlan.getId());
            }
        }
        // 调用NursingPlanMapper来新增计划的基本内容
        nursingPlanMapper.add(nursingProjectPlanVoList);

    }

    //根据id查询护理计划
    @Override
    public NursingPlanVo findById(Integer id) {

        NursingPlanVo byId = nursingPlanMapper.findById(id);

        return byId;
    }
    //修改护理计划

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        nursingPlanMapper.updateById(nursingPlan);
        //2.删除护理项目

        List<Long> ids = new ArrayList<Long>();
        ids.add(nursingPlan.getId());
        nursingProjectPlanMapper.deleteIds(ids);

        //添加护理计划的护理项目
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(nursingProjectPlanVoList)) {
            for (NursingProjectPlanVo projectPlan : nursingProjectPlanVoList) {
                projectPlan.setCreateTime(LocalDateTime.now());
                projectPlan.setPlanId(nursingPlan.getId());
            }
        }
        nursingProjectPlanMapper.insete(nursingProjectPlanVoList);


    }
    //修改护理计划状态

    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);


    }
    //查询所有护理计划

    @Override
    public List <NursingPlanVo>  findAll() {
        List <NursingPlanVo>  nursingPlanVo = nursingPlanMapper.findAll();
        return nursingPlanVo;
    }

    @Override
    public void deleteId(Integer id) {
        nursingPlanMapper.deleteId(id);
    }
}
